package com.lzy.masshappy.aop.log;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Aspect
@Component
public class ApiLogAspect {
    @Pointcut("@annotation(com.lzy.masshappy.aop.log.ApiLog)")
    void apiLogPointcut() {}

    @Pointcut("execution(* com.lzy.masshappy.controller.*.*(..))")
    void controllerPointcut() {}

    @Around("controllerPointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //  日志打印
        //  获取目标对象
        Object target = joinPoint.getTarget();
        //  获取类名
        String className = target.getClass().getName();
        //  获取方法名
        String methodName = joinPoint.getSignature().getName();
        //  获取参数
        Object[] args = joinPoint.getArgs();
        //  记录开始时间
        long startTime = System.currentTimeMillis();

        log.info("==========>{}#{}({})", className, methodName, args);

        //  执行目标函数
        Object result = joinPoint.proceed(args);

        //  记录结束时间
        long endTime = System.currentTimeMillis();

        log.info("<=========={}#{} complete,expensive {}ms", className, methodName, endTime - startTime);

        return result;
    }
}
